package com.combetter.changxing.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.provider.ContactsContract.Contacts.Data;

import com.combetter.changxing.MyApplication;

import java.util.Set;


@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class SPEngine {
    private static Context sContext;
    private static SPEngine spEngine = new SPEngine();

    public static SPEngine getSPEngine() {
        return spEngine;
    }

    private SharedPreferences sharedPreferences;
    private Editor editor;

    private SPEngine() {
        sContext = MyApplication.getInstance();
        sharedPreferences = sContext.getSharedPreferences("metal360", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    private UserInfo userInfo;

    public UserInfo getUserInfo() {
        if (userInfo == null) {
            synchronized (spEngine) {
                if (userInfo == null) {
                    userInfo = new UserInfo();
                }
            }
        }
        return userInfo;
    }

    public static class UserInfo {
        private SharedPreferences userInfoSP;
        private Editor userInfoEditor;

        private UserInfo() {
            userInfoSP = sContext.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
            userInfoEditor = userInfoSP.edit();
            load();
        }

        private void load() {
            tabIndex = userInfoSP.getInt("tabIndex", 0);
            van = userInfoSP.getString("van","");
        }

        private int tabIndex;  //记录首页tab位置


        private String van;


        public String getVan() {
            return van;
        }

        public void setVan(String van) {
            this.van = van;
        }


        public int getTabIndex() {
            return tabIndex;
        }

        public void setTabIndex(int tabIndex) {
            this.tabIndex = tabIndex;
            userInfoEditor.putInt("tabIndex", tabIndex).apply();
        }


        public void clear() {
            userInfoEditor.clear().apply(); //清除本地用户信息
            tabIndex = 0;
        }


    }

}
